package com.knowledge.plus.web.monitor;

import cn.hutool.http.useragent.UserAgent;
import cn.hutool.http.useragent.UserAgentUtil;
import com.easy.utils.SpringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * @author TPF
 * @since 2024/7/31
 */
@RequestMapping
@RestController
public class MonitorTestController {

    @GetMapping("monitor")
    public String monitor(HttpServletRequest request) {
        // do something...
        SpringUtils.context().publishEvent("执行监听");  // 发布事件
        return "monitor";
    }

    @GetMapping("monitor2")
    public String monitor2(HttpServletRequest request) {
        final UserAgent userAgent = UserAgentUtil.parse(request.getHeader("User-Agent"));
        // 获取客户端操作系统
        String os = userAgent.getOs().getName();
        // 获取客户端浏览器
        String browser = userAgent.getBrowser().getName();
        Map<String, Object> map = new HashMap<>();
        map.put("ip", request.getServerName());
        map.put("port", request.getServerPort());
        map.put("url", request.getRequestURL());
        map.put("os", os);
        map.put("browser", browser);
        SpringUtils.context().publishEvent(map);  // 发布事件
        return "monitor2";
    }

}
